package com.raising.modules.sys.controller;

import com.raising.StaticParam;
import com.raising.framework.controller.BaseController;
import com.raising.framework.entity.ResultVo;
import com.raising.framework.interceptor.Page;
import com.raising.modules.sys.entity.Sword;
import com.raising.modules.sys.service.SwordService;
import com.raising.utils.ExceptionsUtils;
import com.raising.utils.JStringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
* 敏感词表
 控制器
* @author gaoy
* @version 1.0
*/
@Controller
@RequestMapping("${adminPath}/sword")
public class SwordController extends BaseController {

    @Autowired
    private SwordService swordService;

    @ModelAttribute
    public Sword get(@RequestParam(required = false) String id) {
        Sword entity = null;
        if (JStringUtils.isNotBlank(id)) {
            entity = swordService.get(id);
        }
        if (entity == null) {
            entity = new Sword();
        }
        return entity;
    }

    @RequiresPermissions("sys:sword:view")
    @RequestMapping()
    public String list(Sword sword,Model model, Page<Sword> page) {
        page.setEntity(sword);
        model.addAttribute("page", swordService.getPage(page));
        return "sys/sword/list";
    }

    /**
     * 查询敏感词表列表数据【json】
     * @author shimj
     * @datetime 2017/12/18 上午11:20
     */
    @RequiresPermissions("sys:sword:view")
    @RequestMapping(value = "/list.json",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public ResultVo listJson(Sword sword, Model model, Page<Sword> page, Integer pageNo) {
        page.setEntity(sword);
        return new ResultVo(ResultVo.SUCCESS, "成功",swordService.getPage(page));
    }

    /**
     * 跳转到编辑页
     * @author shimj
     * @datetime 2017/12/18 上午11:50
     */
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(Sword sword, Model model) {
        model.addAttribute("sword", sword);
        return "sys/sword/form";
    }

    /**
     * 更新敏感词
     * @author shimj
     * @datetime 2017/12/18 上午11:55
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Sword sword,
                         RedirectAttributes redirectAttributes) {
        sword.setStatus(StaticParam.VALID.toString());
        swordService.save(sword);
        addMessage(redirectAttributes, "修改成功");
        return "redirect:" + adminPath + "/sword";
    }

    /**
     * 跳转到敏感词批量录入页面
     * @author shimj
     * @datetime 2017/12/18 下午13:45
     */
    @RequestMapping(value = "/createMuti", method = RequestMethod.GET)
    public String createMuti(Sword sword, Model model) {
        return "/sys/sword/form_muti";
    }

    /**
     * 批量插入敏感词
     * @author shimj
     * @datetime 2017/12/18 下午13:50
     */
    @RequestMapping(value = "/addMuti", method = RequestMethod.POST)
    public String addMuti(String swordStrs, Model model) {
        try {
            //根据回车截取
            String[] swordAttrs = swordStrs.split("\r\n");
            List<Sword> list = new ArrayList<Sword>();
            Sword sword = new Sword();
            for(int i=0;i<swordAttrs.length;i++){
                sword = new Sword();
                sword.setName(swordAttrs[i]);
                list.add(sword);
            }
            swordService.saveMuti(list);
            return "redirect:" + adminPath + "/sword";
        } catch (Exception e) {
            throw ExceptionsUtils.error(e);
        }
    }

    /**
     * 跳转到文件导入页面
     * @author shimj
     * @datetime 2017/12/18 下午14:00
     */
    @RequestMapping(value = "/createFile", method = RequestMethod.GET)
    public String createFile(Sword sword, Model model) {
        return "/sys/sword/form_file";
    }

    /**
     * 上传txt文件
     * @author shimj
     * @datetime 2017/12/18 下午14:00
     */
    @RequestMapping(value = "/addFile", method = RequestMethod.POST)
    public String uploadFile(
            @RequestParam(value = "file", required = false) MultipartFile file,
            HttpServletResponse response, HttpServletRequest request) {
        ResultVo resultVo = null;
        try {
            InputStreamReader read = new InputStreamReader(file.getInputStream(),"GBK");
            try {
                BufferedReader bufferedReader = new BufferedReader(read);
                String txt = null;
                List<Sword> list = new ArrayList<Sword>();
                Sword sword = new Sword();
                //读取文件，将文件内容放入到set中
                while((txt = bufferedReader.readLine()) != null){
                    sword = new Sword();
                    sword.setName(txt);
                    list.add(sword);
                }
                swordService.saveMuti(list);
            } catch (Exception e) {
                throw ExceptionsUtils.error(e);
            }finally{
                read.close();     //关闭文件流
            }
            resultVo = new ResultVo(ResultVo.SUCCESS, "0", "上传成功",null);
        } catch (Exception e) {
            resultVo = new ResultVo(ResultVo.FAILURE, "-1", "上传失败", null);
            throw ExceptionsUtils.error(e);
        }
        return renderString(response, resultVo);
    }

    /**
     * 删除敏感词
     * @author shimj
     * @datetime 2017/12/18 下午14:20
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(Sword sword, int pageNo,
                         int pageSize, RedirectAttributes redirectAttributes) {
        swordService.delete(sword);
        addMessage(redirectAttributes, "删除成功");
        return "redirect:" + adminPath + "/sword?pageNo="
                + pageNo + "&pageSize=" + pageSize;
    }

}
